/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.kaikeba.co

 */
package com.light.exportproject.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.template.*;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
 * 代码生成
 * @author Laght Grey Memory
 */
@Slf4j
@SuppressWarnings("all")
public class GenUtil {

    // 获取模版数据
    public static Map<String,Object> getGenMap(String groupId, String artifactId, String version, String applicationName) {
        // 存储模版字段数据
        Map<String,Object> genMap = new HashMap<>(4);
        genMap.put("groupId",groupId);
        genMap.put("artifactId",artifactId);
        genMap.put("version",version);
        genMap.put("applicationName",applicationName);
        return genMap;
    }

    public static void genFile(File file, Template template, Map<String, Object> map) throws IOException {
        // 生成目标文件
        Writer writer = null;
        try {
            FileUtil.touch(file);
            writer = new FileWriter(file);
            template.render(map, writer);
        } catch (TemplateException | IOException e) {
            throw new RuntimeException(e);
        } finally {
            assert writer != null;
            writer.close();
        }
    }
}
